package com.guyaning.deyson_core.core.delegates.web;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;

import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.delegates.web.route.RouteKeys;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2018/7/3.
 */

public abstract class WebDelegate extends DeysonDelegate implements IWebviewInilalizer {

    private WebView mWebView = null;

    private final ReferenceQueue<WebView> WEB_QUEUE = new ReferenceQueue<>();

    private String mUrl = null;

    private boolean mIsWebViewAbailable = false;

    private DeysonDelegate mTopDelegate = null;

    public WebDelegate() {

    }

    public abstract IWebviewInilalizer setInilalizer();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();

        mUrl = args.getString(RouteKeys.URL.name());

        initWebView();
    }


    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
        } else {
            final IWebviewInilalizer inilalizer = setInilalizer();

            if (inilalizer != null) {

                final WeakReference<WebView> webViewWeakReference = new WeakReference<>(new WebView(getContext()), WEB_QUEUE);

                mWebView = webViewWeakReference.get();

                mWebView = inilalizer.initWebView(mWebView);

                mWebView.setWebViewClient(inilalizer.initWebViewClient());

                mWebView.setWebChromeClient(inilalizer.initWebChromeClient());

                mWebView.addJavascriptInterface(LatteWebInterface.create(this), "latte");

                mIsWebViewAbailable = true;
            } else {
                throw new NullPointerException("加载器没有正常加载");
            }
        }
    }

    public void setTopDelegate(DeysonDelegate delegate) {
        mTopDelegate = delegate;
    }

    public DeysonDelegate getTopDelegate() {

        if (mTopDelegate == null) {
            mTopDelegate = this;
        }

        return mTopDelegate;
    }

    public WebView getWebView() {
        if (mWebView == null) {
            throw new NullPointerException("WebView is null");
        }

        return mIsWebViewAbailable ? mWebView : null;
    }

    public String getWebUrl() {
        if (mUrl == null) {
            throw new NullPointerException("Url is null");
        }

        return mUrl;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mWebView != null) {
            mWebView.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mWebView != null) {
            mWebView.onResume();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsWebViewAbailable = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
    }
}
